<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>关于请求 | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C05-Flask-RESTful/AboutResponse.html" />
    
    
    <link rel="prev" href="../C05-Flask-RESTful/AboutView.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="6.3"
        data-chapter-title="关于请求"
        data-filepath="C05-Flask-RESTful/AboutRequest.md"
        data-basepath=".."
        data-revision="Sat May 11 2019 20:06:35 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutFlask.html">
            
                
                    <a href="../AboutFlask.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/HelloWorld.html">
            
                
                    <a href="../C01-CreateProject/HelloWorld.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        HelloWorld程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/AboutParams.html">
            
                
                    <a href="../C01-CreateProject/AboutParams.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        参数说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/StartDevelopmentServer.html">
            
                
                    <a href="../C01-CreateProject/StartDevelopmentServer.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        开发服务器运行方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-RouterAndBlueprint/index.html">
            
                
                    <a href="../C02-RouterAndBlueprint/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        路由与蓝图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-RouterAndBlueprint/Router.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Router.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        路由
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-RouterAndBlueprint/Blueprint.html">
            
                
                    <a href="../C02-RouterAndBlueprint/Blueprint.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        蓝图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/HandleRequest.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        处理请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C03-RequestAndResponse/HandleResponse.html">
            
                
                    <a href="../C03-RequestAndResponse/HandleResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        处理响应
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/CookieAndSession.html">
            
                
                    <a href="../C03-RequestAndResponse/CookieAndSession.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie与Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-HookAndContext/index.html">
            
                
                    <a href="../C04-HookAndContext/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        请求钩子与上下文
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-HookAndContext/AbortAndErrorhandler.html">
            
                
                    <a href="../C04-HookAndContext/AbortAndErrorhandler.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        异常处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-HookAndContext/Hook.html">
            
                
                    <a href="../C04-HookAndContext/Hook.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        请求钩子
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C04-HookAndContext/Context.html">
            
                
                    <a href="../C04-HookAndContext/Context.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上下文
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Flask-RESTful/index.html">
            
                
                    <a href="../C05-Flask-RESTful/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        Flask-RESTful
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Flask-RESTful/QuickStart.html">
            
                
                    <a href="../C05-Flask-RESTful/QuickStart.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        起步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C05-Flask-RESTful/AboutView.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        关于视图
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.3" data-path="C05-Flask-RESTful/AboutRequest.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutRequest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        关于请求
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C05-Flask-RESTful/AboutResponse.html">
            
                
                    <a href="../C05-Flask-RESTful/AboutResponse.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        关于响应
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5173;&#x4E8E;&#x8BF7;&#x6C42;&#x5904;&#x7406;">&#x5173;&#x4E8E;&#x8BF7;&#x6C42;&#x5904;&#x7406;</h1>
<p>Flask-RESTful &#x63D0;&#x4F9B;&#x4E86;<code>RequestParser</code>&#x7C7B;&#xFF0C;&#x7528;&#x6765;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x68C0;&#x9A8C;&#x548C;&#x8F6C;&#x6362;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask_restful <span class="hljs-keyword">import</span> reqparse

parser = reqparse.RequestParser()
parser.add_argument(<span class="hljs-string">&apos;rate&apos;</span>, type=int, help=<span class="hljs-string">&apos;Rate cannot be converted&apos;</span>, location=<span class="hljs-string">&apos;args&apos;</span>)
parser.add_argument(<span class="hljs-string">&apos;name&apos;</span>)
args = parser.parse_args()
</code></pre>
<h4 id="&#x4F7F;&#x7528;&#x6B65;&#x9AA4;&#xFF1A;">&#x4F7F;&#x7528;&#x6B65;&#x9AA4;&#xFF1A;</h4>
<ol>
<li><p>&#x521B;&#x5EFA;<code>RequestParser</code>&#x5BF9;&#x8C61;</p>
</li>
<li><p>&#x5411;<code>RequestParser</code>&#x5BF9;&#x8C61;&#x4E2D;&#x6DFB;&#x52A0;&#x9700;&#x8981;&#x68C0;&#x9A8C;&#x6216;&#x8F6C;&#x6362;&#x7684;&#x53C2;&#x6570;&#x58F0;&#x660E;</p>
</li>
<li><p>&#x4F7F;&#x7528;<code>parse_args()</code>&#x65B9;&#x6CD5;&#x542F;&#x52A8;&#x68C0;&#x9A8C;&#x5904;&#x7406;</p>
</li>
<li><p>&#x68C0;&#x9A8C;&#x4E4B;&#x540E;&#x4ECE;&#x68C0;&#x9A8C;&#x7ED3;&#x679C;&#x4E2D;&#x83B7;&#x53D6;&#x53C2;&#x6570;&#x65F6;&#x53EF;&#x6309;&#x7167;&#x5B57;&#x5178;&#x64CD;&#x4F5C;&#x6216;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x64CD;&#x4F5C;</p>
<pre><code class="lang-python">args.rate
&#x6216;
args[<span class="hljs-string">&apos;rate&apos;</span>]
</code></pre>
</li>
</ol>
<h2 id="&#x53C2;&#x6570;&#x8BF4;&#x660E;">&#x53C2;&#x6570;&#x8BF4;&#x660E;</h2>
<h3 id="1-required">1 required</h3>
<p>&#x63CF;&#x8FF0;&#x8BF7;&#x6C42;&#x662F;&#x5426;&#x4E00;&#x5B9A;&#x8981;&#x643A;&#x5E26;&#x5BF9;&#x5E94;&#x53C2;&#x6570;&#xFF0C;<strong>&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;False</strong></p>
<ul>
<li><p>True  &#x5F3A;&#x5236;&#x8981;&#x6C42;&#x643A;&#x5E26;</p>
<p>&#x82E5;&#x672A;&#x643A;&#x5E26;&#xFF0C;&#x5219;&#x6821;&#x9A8C;&#x5931;&#x8D25;&#xFF0C;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x8FD4;&#x56DE;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#xFF0C;&#x72B6;&#x6001;&#x7801;400</p>
</li>
<li><p>False &#x4E0D;&#x5F3A;&#x5236;&#x8981;&#x6C42;&#x643A;&#x5E26;</p>
<p>&#x82E5;&#x4E0D;&#x5F3A;&#x5236;&#x643A;&#x5E26;&#xFF0C;&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;&#x672A;&#x643A;&#x5E26;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x53D6;&#x51FA;&#x503C;&#x4E3A;None</p>
</li>
</ul>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoResource</span><span class="hljs-params">(Resource)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        rp = RequestParser()
        rp.add_argument(<span class="hljs-string">&apos;a&apos;</span>, required=<span class="hljs-keyword">False</span>)
        args = rp.parse_args()
        <span class="hljs-keyword">return</span> {<span class="hljs-string">&apos;msg&apos;</span>: <span class="hljs-string">&apos;data={}&apos;</span>.format(args.a)}
</code></pre>
<h3 id="2-help">2 help</h3>
<p>&#x53C2;&#x6570;&#x68C0;&#x9A8C;&#x9519;&#x8BEF;&#x65F6;&#x8FD4;&#x56DE;&#x7684;&#x9519;&#x8BEF;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;</p>
<pre><code class="lang-python">rp.add_argument(<span class="hljs-string">&apos;a&apos;</span>, required=<span class="hljs-keyword">True</span>, help=<span class="hljs-string">&apos;missing a param&apos;</span>)
</code></pre>
<h3 id="3-action">3 action</h3>
<p>&#x63CF;&#x8FF0;&#x5BF9;&#x4E8E;&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x4E2D;&#x51FA;&#x73B0;&#x591A;&#x4E2A;&#x540C;&#x540D;&#x53C2;&#x6570;&#x65F6;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;</p>
<ul>
<li><code>action=&apos;store&apos;</code>  &#x4FDD;&#x7559;&#x51FA;&#x73B0;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#xFF0C; &#x9ED8;&#x8BA4;</li>
<li><code>action=&apos;append&apos;</code> &#x4EE5;&#x5217;&#x8868;&#x8FFD;&#x52A0;&#x4FDD;&#x5B58;&#x6240;&#x6709;&#x540C;&#x540D;&#x53C2;&#x6570;&#x7684;&#x503C;</li>
</ul>
<pre><code class="lang-python">rp.add_argument(<span class="hljs-string">&apos;a&apos;</span>, required=<span class="hljs-keyword">True</span>, help=<span class="hljs-string">&apos;missing a param&apos;</span>, action=<span class="hljs-string">&apos;append&apos;</span>)
</code></pre>
<h3 id="4-type">4 type</h3>
<p>&#x63CF;&#x8FF0;&#x53C2;&#x6570;&#x5E94;&#x8BE5;&#x5339;&#x914D;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;python&#x7684;&#x6807;&#x51C6;&#x6570;&#x636E;&#x7C7B;&#x578B;string&#x3001;int&#xFF0C;&#x4E5F;&#x53EF;&#x4F7F;&#x7528;Flask-RESTful&#x63D0;&#x4F9B;&#x7684;&#x68C0;&#x9A8C;&#x65B9;&#x6CD5;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x5B9A;&#x4E49;</p>
<ul>
<li><p>&#x6807;&#x51C6;&#x7C7B;&#x578B;</p>
<pre><code class="lang-python">rp.add_argument(<span class="hljs-string">&apos;a&apos;</span>, type=int, required=<span class="hljs-keyword">True</span>, help=<span class="hljs-string">&apos;missing a param&apos;</span>, action=<span class="hljs-string">&apos;append&apos;</span>)
</code></pre>
</li>
<li><p>Flask-RESTful&#x63D0;&#x4F9B;</p>
<p>&#x68C0;&#x9A8C;&#x7C7B;&#x578B;&#x65B9;&#x6CD5;&#x5728;<code>flask_restful.inputs</code>&#x6A21;&#x5757;&#x4E2D;</p>
<ul>
<li><p><code>url</code></p>
</li>
<li><p><code>regex(&#x6307;&#x5B9A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;)</code>  </p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask_restful <span class="hljs-keyword">import</span> inputs
rp.add_argument(<span class="hljs-string">&apos;a&apos;</span>, type=inputs.regex(<span class="hljs-string">r&apos;^\d{2}&amp;&apos;</span>))
</code></pre>
</li>
<li><p><code>natural</code>  &#x81EA;&#x7136;&#x6570;0&#x3001;1&#x3001;2&#x3001;3...</p>
</li>
<li><p><code>positive</code>  &#x6B63;&#x6574;&#x6570; 1&#x3001;2&#x3001;3...</p>
</li>
<li><p><code>int_range(low ,high)</code>  &#x6574;&#x6570;&#x8303;&#x56F4;</p>
<pre><code class="lang-python">rp.add_argument(<span class="hljs-string">&apos;a&apos;</span>, type=inputs.int_range(<span class="hljs-number">1</span>, <span class="hljs-number">10</span>))
</code></pre>
</li>
<li><p><code>boolean</code></p>
</li>
</ul>
</li>
<li><p>&#x81EA;&#x5B9A;&#x4E49;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">mobile</span><span class="hljs-params">(mobile_str)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x68C0;&#x9A8C;&#x624B;&#x673A;&#x53F7;&#x683C;&#x5F0F;
    :param mobile_str: str &#x88AB;&#x68C0;&#x9A8C;&#x5B57;&#x7B26;&#x4E32;
    :return: mobile_str
    &quot;&quot;&quot;</span>
    <span class="hljs-keyword">if</span> re.match(<span class="hljs-string">r&apos;^1[3-9]\d{9}$&apos;</span>, mobile_str):
        <span class="hljs-keyword">return</span> mobile_str
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">&apos;{} is not a valid mobile&apos;</span>.format(mobile_str))

rp.add_argument(<span class="hljs-string">&apos;a&apos;</span>, type=mobile)
</code></pre>
</li>
</ul>
<h3 id="5-location">5 location</h3>
<p>&#x63CF;&#x8FF0;&#x53C2;&#x6570;&#x5E94;&#x8BE5;&#x5728;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x4F4D;&#x7F6E;</p>
<pre><code class="lang-python"><span class="hljs-comment"># Look only in the POST body</span>
parser.add_argument(<span class="hljs-string">&apos;name&apos;</span>, type=int, location=<span class="hljs-string">&apos;form&apos;</span>)

<span class="hljs-comment"># Look only in the querystring</span>
parser.add_argument(<span class="hljs-string">&apos;PageSize&apos;</span>, type=int, location=<span class="hljs-string">&apos;args&apos;</span>)

<span class="hljs-comment"># From the request headers</span>
parser.add_argument(<span class="hljs-string">&apos;User-Agent&apos;</span>, location=<span class="hljs-string">&apos;headers&apos;</span>)

<span class="hljs-comment"># From http cookies</span>
parser.add_argument(<span class="hljs-string">&apos;session_id&apos;</span>, location=<span class="hljs-string">&apos;cookies&apos;</span>)

<span class="hljs-comment"># From json</span>
parser.add_argument(<span class="hljs-string">&apos;user_id&apos;</span>, location=<span class="hljs-string">&apos;json&apos;</span>)

<span class="hljs-comment"># From file uploads</span>
parser.add_argument(<span class="hljs-string">&apos;picture&apos;</span>, location=<span class="hljs-string">&apos;files&apos;</span>)
</code></pre>
<p>&#x4E5F;&#x53EF;&#x6307;&#x660E;&#x591A;&#x4E2A;&#x4F4D;&#x7F6E;</p>
<pre><code class="lang-python">parser.add_argument(<span class="hljs-string">&apos;text&apos;</span>, location=[<span class="hljs-string">&apos;headers&apos;</span>, <span class="hljs-string">&apos;json&apos;</span>])
</code></pre>
<h4 id=""> </h4>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C05-Flask-RESTful/AboutView.html" class="navigation navigation-prev " aria-label="Previous page: 关于视图"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C05-Flask-RESTful/AboutResponse.html" class="navigation navigation-next " aria-label="Next page: 关于响应"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
